﻿@page "/admin/listcomments"
@attribute [Authorize(Roles = "Admin")]

@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="评论版块" />

<MDCard>
    <DataTableCard TModel="CommentOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl)" Title="@($"{_name}列表")" ShowAddButton="false" @ref="dataTableCard">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <div style="width: 100px;">
                    <MIcon Color="success"
                           Class="mr-2"
                           OnClick="()=>EditItemPriority(context.Item,true)">@IconType.Up.ToIconString()</MIcon>
                    <MIcon Color="info"
                           Class="mr-2"
                           OnClick="()=>EditItemPriority(context.Item,false)">@IconType.Down.ToIconString()</MIcon>
                    @if (context.Item.IsHidden)
                    {
                        <MIcon Color="warning"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,false)">@IconType.Check.ToIconString()</MIcon>
                    }
                    else
                    {
                        <MIcon Color="error"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,true)">@IconType.Close.ToIconString()</MIcon>
                    }
                </div>
            }
            else if (context.Header.Value == nameof(CommentOverviewModel.IsHidden))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Recommends.StateChip IsHidden="context.Item.IsHidden" />
            }
            else if (context.Header.Value == nameof(CommentOverviewModel.ObjectName))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Comments.ObjectChip Id="@context.Item.ObjectId" Name="@context.Item.ObjectName" Type="context.Item.Type" />
            }
            else if (context.Header.Value == nameof(CommentOverviewModel.UserName))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.EditRecords.UserCenter.ReviewEditRecords.UserChip Id="@context.Item.UserId" Name="@context.Item.UserName" />
            }
            else if (context.Header.Value == nameof(CommentOverviewModel.CommentTime))
            {
                @context.Item.CommentTime.ToString("yyyy-MM-dd HH:mm")
            }
            else if (context.Header.Value == nameof(CommentOverviewModel.Text))
            {
                <div style="white-space: pre-line;width: 300px;word-break: break-all;">
                    @context.Item.Text
                </div>
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>


@code {
    private string _baseUrl = "api/comments/List";
    private string _name = "评论";

    private List<DataTableHeader<CommentOverviewModel>> _headers = new List<DataTableHeader<CommentOverviewModel>>
{
          new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(CommentOverviewModel.Id)
          },
          new (){ Text= "时间", Value= nameof(CommentOverviewModel.CommentTime)},
          new (){ Text= "用户", Value= nameof(CommentOverviewModel.UserName)},
          new (){ Text= "内容", Value= nameof(CommentOverviewModel.Text)},
          new (){ Text= "目标", Value= nameof(CommentOverviewModel.ObjectName)},
          new (){ Text= "状态", Value= nameof(CommentOverviewModel.IsHidden)},
          new (){ Text= "优先级", Value= nameof(CommentOverviewModel.Priority)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<CommentOverviewModel> dataTableCard;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    public async Task EditItemPriority(CommentOverviewModel model, bool up)
    {
        string operationName = up ? "增权重" : "降权重";
        try
        {
            var obj = await _httpService.PostAsync<EditCommentPriorityViewModel, Result>("api/comments/EditCommentPriority", new EditCommentPriorityViewModel { Ids = new long[]{model.Id}, PlusPriority = up?1:-1 });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败",  AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }

    public async Task HideItem(CommentOverviewModel model, bool isHidden)
    {
        string operationName = isHidden ? "隐藏评论" : "显示评论";
        try
        {
            var obj = await _httpService.PostAsync<HiddenCommentModel, Result>("api/comments/HiddenComment", new HiddenCommentModel { Ids = new long[] { model.Id }, IsHidden = isHidden });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败",  AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }
}
